package cre.sample.test.gc.oom;

import sun.misc.Unsafe;

import java.lang.reflect.Field;

/**
 * 直接内存，使用unsafe.allocateMemory()
 * VM Args: -Xmx20M -XX:MaxDirectMemorySize=10M
 * Created by CreGu on 2016/5/24.
 */
public class DirectMemoryOOM {
    private static final int _1MB = 1024 * 1024;

    public static void main(String[] args) {
        try {
            Field unsafeField = Unsafe.class.getDeclaredField("theUnsafe");
            unsafeField.setAccessible(true);

            Unsafe unsafe = (Unsafe) unsafeField.get(null);
            while (true) {
                unsafe.allocateMemory(_1MB);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
